package edu.bw;

public class ThreadLocalDemo {
    //创建一个ThreadLocal变量 实现每个线程都有其单独的String对象
    private static ThreadLocal<String> threadLocal=new ThreadLocal();

    public static void main(String[] args) {
        //创建线程Thread-01 绑定自己的String对象
        Thread thread01 =new Thread(()->{
            threadLocal.set("我是线程01的数据");
            System.out.println(Thread.currentThread().getName()+" 获取的数据为："+threadLocal.get());
        });

        Thread thread02=new Thread(()->{
            threadLocal.set("我是线程02的数据");
            System.out.println(Thread.currentThread().getName()+" 获取的数据为："+threadLocal.get());
        },"thread01");

        thread01.start();
        System.out.println("-----------------------------------------");
        thread02.start();
    }
}
